package com.example.a2209_mianshi.ui.mall

import android.content.Context
import android.os.Handler
import android.util.Log
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.example.a2209_mianshi.R
import com.example.a2209_mianshi.base.BaseActivity
import com.example.a2209_mianshi.databinding.ActivityDemoBinding
import com.example.a2209_mianshi.ui.mall.demo.HomeAdapter
import com.example.a2209_mianshi.ui.mall.demo.HomeItem
import com.example.a2209_mianshi.ui.mall.model.MallGoods
import com.example.a2209_mianshi.ui.mall.model.MallHomeRowItem
import com.example.a2209_mianshi.ui.mall.viewModel.MallViewModel
import com.youth.banner.loader.ImageLoader
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch


@AndroidEntryPoint
class DemoActivity : BaseActivity<MallViewModel,ActivityDemoBinding>() {
    private val viewModel : MallViewModel by viewModels()
    var list= mutableListOf<String>()
    var data= mutableListOf<MallHomeRowItem>()
    override val mLayoutResId: Int
        get() = R.layout.activity_demo
    var mAdapter: DemoAdapter? = null
    private lateinit var adapter: HomeAdapter
    override fun initView() {
        fetchHomeData(viewModel){
            Log.d("demo", "initListener: "+ it)
            adapter = HomeAdapter(it)
            mBinding.rec.adapter=adapter
            mBinding.rec.layoutManager = LinearLayoutManager(this)
        }
//        mAdapter = DemoAdapter()
//        mBinding.rec.adapter=mAdapter
//        mBinding.rec.layoutManager= GridLayoutManager(this,2)
    }
    override fun initData() {
        viewModel.cmallAdInfo()
        viewModel.cmallBannerInfo()
    }

    override fun initListener() {
//        if (!fetchHomeData(viewModel).isNotEmpty()){
//            Log.d("demo", "initListener: "+ fetchHomeData(viewModel))
//        }
    }
    fun fetchHomeData(vm: MallViewModel,onComplete: (List<HomeItem>) -> Unit){
        val homeItems = mutableListOf<HomeItem>()
//状态管理
        addCommonStateListener(viewModel.bannerMallInfoStateFlow)
        // 1. 添加轮播图数据
        vm.bannerMallInfoLiveData.observe(this){
            homeItems.add(HomeItem.BannerItem(it))
            Log.d("demo1", "initListener: "+ homeItems)
//            it.forEach {
//                list.add(it.imageUrl)
//            }
//            mBinding.banner.setImages(list)
//            mBinding.banner.setImageLoader(object : ImageLoader(){
//                override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
//                    if (imageView != null) {
//                        if (context != null) {
//                            Glide.with(context).load(path).into(imageView)
//                        }
//                    }
//                }
//            })
//            mBinding.banner.start()
        }
        addCommonStateListener(viewModel.mallMallGroupStateFlow)
        viewModel.mallAdInfoLiveData.observe(this){
            homeItems.add(HomeItem.GoodsItem(it))
            onComplete(homeItems) // 数据就绪后回调
            Log.d("demo2", "initListener: "+ homeItems)
//            it.forEach {item->
//                mAdapter?.data?.clear()
//                mAdapter?.data?.addAll(item.mallGoodsInfoPageResponseList)
//                mAdapter?.notifyDataSetChanged()
//            }
        }
//        lifecycleScope.launch {
//
//        }
//        val handler =  Handler()
//        handler.postDelayed( Runnable {
//
//        },5000)
    }
}